﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
#if UNITY_EDITOR
using UnityEditor;
#endif

#if UNITY_EDITOR
[ExecuteInEditMode]
#endif
[RequireComponent(typeof(SpriteMask))]
public class SpritePatternModifier : MonoBehaviour
{
    private SpriteMask s;
    private Tilemap t;
    public Sprite pattern;
    private Texture2D patternTexture;
    private Texture2D atlas;
    private float scaleX;
    private float scaleY;
    private Vector3 mapCenter;

    [Header("地图单元格尺寸X")]
    public float tileSizeX = 148;
    [Header("地图单元格尺寸Y")]
    public float tileSizeY = 196;
    [Header("迷雾单元格尺寸X")]
    public float fogSizeX = 27;
    [Header("迷雾单元格尺寸Y")]
    public float fogSizeY = 32.8f;
    [Header("对其到中心")]
    public bool setToCenter = false;

    private void Awake()
    {
        Transform parent = this.transform;

        s = this.gameObject.GetComponent<SpriteMask>();
        t = this.GetComponentInParent<Tilemap>();
        if (!t) return;

        mapCenter = t.CellToLocal(new Vector3Int((int)t.cellBounds.center.x, (int)t.cellBounds.center.y,0));

        patternTexture = pattern.texture;

        scaleX = tileSizeX / patternTexture.width;// 单元格尺寸/印花贴图尺寸 = 印花缩放系数
        scaleY = tileSizeY / patternTexture.height;
        if(setToCenter) transform.localPosition = mapCenter;

        Color[] colors = patternTexture.GetPixels();
        atlas = new Texture2D((int)((t.localBounds.size.x + 2) * tileSizeX/4), (int)((t.localBounds.size.y + 2) * tileSizeY / 4), TextureFormat.Alpha8, false, false);

        Color[] alpha = new Color[atlas.width * atlas.height];
        //TODO 设置alpha有没有更好的方法
        for (int i = 0; i < alpha.Length; i++)
        {
            alpha[i].a = 0;
        }

        atlas.SetPixels(0, 0, atlas.width, atlas.height, alpha);
        atlas.Apply();

        Vector2 atlasSize = new Vector2(atlas.width, atlas.height);
        Rect spriteRect = new Rect(new Vector2(0, 0), atlasSize);
        Sprite sprite = Sprite.Create(atlas, spriteRect, new Vector2(0.5f, 0.5f));

        s.sprite = sprite;

        transform.localScale = new Vector3(scaleX, scaleY, 0);

        ApplyTexture();
    }

    private void OnDestroy()
    {
        Sprite sprite = s.sprite;
        s.sprite = null;
        GameObject.Destroy(sprite);

        GameObject.Destroy(atlas);
        atlas = null;
    }

    public void UpdateTextureByCell(int x, int y)
    {
        Color[] colors = patternTexture.GetPixels();
        Vector3 cellPos = t.CellToLocal(new Vector3Int(x,y,0)) - mapCenter;//;格子加算了cellSize的坐标
        //坐标换算成像素
        AppendPattern(s.sprite.texture.width / 2 + Mathf.RoundToInt(cellPos.x * fogSizeX),
            s.sprite.texture.height / 2 + Mathf.RoundToInt(cellPos.y * fogSizeY));
    }

    public void ApplyTexture()
    {
        if(s && s.sprite && s.sprite.texture)
        {
            s.sprite.texture.Apply();
        }
    }

    //增量方式印花，只把把非显示变为显示
    private void AppendPattern(int x,int y)
    {
        Texture2D t = s.sprite.texture;
        x -= patternTexture.width/2;
        y -= patternTexture.height/2;
        for (int i = 0;i < patternTexture.width; i++)
        {
            for (int j = 0; j < patternTexture.height; j++)
            {
                Color c = t.GetPixel(x+i, y+j);
                Color o = patternTexture.GetPixel(i, j);
;               if (o.a >= 0.2 && c.a < 0.2)
                {
                    s.sprite.texture.SetPixel(x+i, y+j, Color.white);
                }
            }
        }
    }

}
